package org.soc.timesheet.domain;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

@Configurable
@Entity
@Table(name = "SUB_FUNCTIONS")
@RooJavaBean
@RooJpaActiveRecord(versionField = "", table = "SUB_FUNCTIONS")
@RooDbManaged(automaticallyDelete = true)
@RooToString(excludeFields = { "roleses", "idFunctions" })
@RooJson
@JsonIgnoreProperties({ "roleses", "idFunctions" })
public class SubFunctions {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Integer id;

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @ManyToOne
    @JoinColumn(name = "ID_FUNCTIONS", referencedColumnName = "ID", nullable = false)
    private Functions idFunctions;

    @Column(name = "DESC_SUB_FUNCTIONS", length = 100)
    private String descSubFunctions;

    public Set<Roles> getRoleses() {
        return roleses;
    }

    public void setRoleses(Set<Roles> roleses) {
        this.roleses = roleses;
    }

    public Functions getIdFunctions() {
        return idFunctions;
    }

    public void setIdFunctions(Functions idFunctions) {
        this.idFunctions = idFunctions;
    }

    public String getDescSubFunctions() {
        return descSubFunctions;
    }

    public void setDescSubFunctions(String descSubFunctions) {
        this.descSubFunctions = descSubFunctions;
    }

    public String toString() {
        return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames("roleses", "idFunctions").toString();
    }

    public String toJson() {
        return new JSONSerializer().exclude("*.class").serialize(this);
    }

    public String toJson(String[] fields) {
        return new JSONSerializer().include(fields).exclude("*.class").serialize(this);
    }

    public static SubFunctions fromJsonToSubFunctions(String json) {
        return new JSONDeserializer<SubFunctions>().use(null, SubFunctions.class).deserialize(json);
    }

    public static String toJsonArray(Collection<SubFunctions> collection) {
        return new JSONSerializer().exclude("*.class").serialize(collection);
    }

    public static String toJsonArray(Collection<SubFunctions> collection, String[] fields) {
        return new JSONSerializer().include(fields).exclude("*.class").serialize(collection);
    }

    public static Collection<SubFunctions> fromJsonArrayToSubFunctionses(String json) {
        return new JSONDeserializer<List<SubFunctions>>().use("values", SubFunctions.class).deserialize(json);
    }

    @PersistenceContext
    transient EntityManager entityManager;

    public static final List<String> fieldNames4OrderClauseFilter = java.util.Arrays.asList("");

    public static final EntityManager entityManager() {
        EntityManager em = new SubFunctions().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

    public static long countSubFunctionses() {
        return entityManager().createQuery("SELECT COUNT(o) FROM SubFunctions o", Long.class).getSingleResult();
    }

    public static List<SubFunctions> findAllSubFunctionses() {
        return entityManager().createQuery("SELECT o FROM SubFunctions o", SubFunctions.class).getResultList();
    }

    public static List<SubFunctions> findAllSubFunctionses(String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM SubFunctions o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, SubFunctions.class).getResultList();
    }

    public static SubFunctions findSubFunctions(Integer id) {
        if (id == null) return null;
        return entityManager().find(SubFunctions.class, id);
    }

    public static List<SubFunctions> findSubFunctionsEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM SubFunctions o", SubFunctions.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    public static List<SubFunctions> findSubFunctionsEntries(int firstResult, int maxResults, String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM SubFunctions o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, SubFunctions.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

    @Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            SubFunctions attached = SubFunctions.findSubFunctions(this.id);
            this.entityManager.remove(attached);
        }
    }

    @Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

    @Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

    @Transactional
    public SubFunctions merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        SubFunctions merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

    @ManyToMany
    @JoinTable(name = "SUB_FUNCTIONS_ROLES", joinColumns = { @javax.persistence.JoinColumn(name = "ID_SUB_FUNCTION", nullable = false) }, inverseJoinColumns = { @javax.persistence.JoinColumn(name = "ID_ROLE", nullable = false) })
    private Set<Roles> roleses;
}
